package eip.chapter3.pubsub.impl;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOException;

import eip.chapter3.pubsub.HotelDistributionService;
import eip.chapter3.pubsub.RegulationUpdate;

/**
 * Implementation class for the HotelDistributionService. This class makes use
 * of the muleclient to send a number of messages on the ESB.
 * 
 * @author nl24167
 */
public class HotelDistributionServiceImpl implements HotelDistributionService {

	private final static Logger LOG = Logger.getLogger(HotelDistributionServiceImpl.class);

	private final static String REGULATIONS_UPDATE = "regulations-update";
	private final static String REGULATION = "regulation";
	private final static String ID = "id";
	private final static String NAME = "name";
	private final static String DESCRIPTION = "description";
	private final static String SB_NAMESPACE = "http://www.sleepingbeauty.com/hotelregulations";
	
	private String target;

	/**
	 * The method that is used to publish the updates.
	 */
	public void publishUpdates(List<RegulationUpdate> updates) {
		try {
			String message = createXMLRegulationMessage(updates); 
			MuleClient client;
			LOG.info("Sending message " + message + " to " + target);
			client = new MuleClient();
			client.dispatch(target,message,null);
		} catch (UMOException umoE) {
			LOG.error("Error sending message to target: " + target,umoE);
		} catch (IOException ioExc) {
			LOG.error("Error creating XML message",ioExc);
		}
	}
	
	//---------------------------------------------
	// Private helper methods
	//---------------------------------------------
	
	private String createXMLRegulationMessage(List<RegulationUpdate> regulations) throws IOException {
	    Element root = DocumentHelper.createElement(REGULATIONS_UPDATE);
	    root.addNamespace("",SB_NAMESPACE);
	    
	    for (RegulationUpdate update : regulations) {
			Element regulation = DocumentHelper.createElement(REGULATION);
			Element name = DocumentHelper.createElement(NAME);
			name.setText(update.getName());
			Element description = DocumentHelper.createElement(DESCRIPTION);
			description.setText(update.getDescription());
			
			regulation.add(DocumentHelper.createAttribute(regulation,ID,Integer.toString(update.getId())));
			regulation.add(name);
			regulation.add(description);
			
			root.add(regulation);
		}
	    XMLWriter writer = new XMLWriter(OutputFormat.createPrettyPrint());
	    StringWriter stringWriter = new StringWriter();
	    writer.setWriter(stringWriter);
	    writer.write(root);
	    return stringWriter.toString();
	}
	
	
	//---------------------------------------------
	// Simple setters and getters
	//---------------------------------------------

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
}
